package com.small.sentinel;

import com.small.sentinel.node.DefaultNode;
import com.small.sentinel.node.Node;

public class Context {

    /**
     * 上下文名称
     */
    private String name;

    /**
     * 入口节点
     */
    private DefaultNode entranceNode;

    /**
     * 当前入口
     */
    private Entry curEntry;

    /**
     * 来源
     */
    private String origin = "";

    public Context(DefaultNode entranceNode, String name) {
        this.name = name;
        this.entranceNode = entranceNode;
    }

    public Context setCurEntry(Entry curEntry) {
        this.curEntry = curEntry;
        return this;
    }

    public Context setCurNode(Node node) {
        this.curEntry.setCurNode(node);
        return this;
    }

    public String getName() {
        return name;
    }

    public DefaultNode getEntranceNode() {
        return entranceNode;
    }

    public Entry getCurEntry() {
        return curEntry;
    }
    public Node getCurNode() {
        return curEntry == null ? null : curEntry.getCurNode();
    }

    public Node getLastNode() {
        if (curEntry != null && curEntry.getLastNode() != null) {
            return curEntry.getLastNode();
        } else {
            return entranceNode;
        }
    }

    public String getOrigin() {
        return origin;
    }

    public Context setOrigin(String origin) {
        this.origin = origin;
        return this;
    }
}
